<!DOCTYPE html>
<html>
<head>
<head/>
<div th:include="base-libs-easyui :: base-libs-easyui" />
<title>医疗机构人员列表</title>
<style type="text/css">
body{
	padding: 0px 10px;
}
</style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${requestData}]];
</script>
<script type="text/javascript">
$(function() {
 	initView();
    $(this).registerManagerPage({
        cacheTables: ["TB_ZY_XB"]
        ,callback: initData
    });
});

function initData(cacheData){
    $("body").data("cacheData", cacheData);
	if(!requestData["isManager"]){
        var PTJGID = requestData["PTJGID"];
        var YLJGMC = requestData["YLJGMC"];
        var value = {PTJGID: PTJGID, YLJGMC: YLJGMC};
        $("#combogridYljg").combogrid("grid").datagrid("loadData", [value]);
        $("#combogridYljg").combogrid("setValue", value);
    }
    $("#btnQuery").trigger("click");
}

function queryForm() {
    $("#dgForm").datagrid({url: contextRoot + "/yljgry/queryPageYljgry.service"});
}

function initView()
{
	$("#dgForm").datagrid({
		idField : "USERID",
		striped : true,
        fitColumns : true,
        fit : true,
        border : false,
        rownumbers: true,
        singleSelect: true,
		toolbar : "#tb",
        pagination: true,
        loadMsg: "",
		columns : [[
			{
				field : "YLJGMC",
				title : "医疗机构名称"
				,width: 4
			},
			{
				field : "KSMC",
				title : "科室名称"
                ,width: 4
			},
            {
                field : "YSGH",
                title : "工号"
                ,width: 2
            },
			{
				field : "XM",
				title : "姓名"
                ,width: 4
			},
			{
				field : "XB",
				title : "性别"
                ,width: 2
				,formatter : function(value, row, index){
				    var text = "";
				    var cacheData = $("body").data("cacheData");
				    if(cacheData){
                        var xbCache = cacheData["TB_ZY_XB"];
                        if(xbCache){
                            $.each(xbCache, function(index, item){
								var XMDM = item["XMDM"];
								if(XMDM == value){
                                    text = item["XMMC"];
                                    return false;
								}
                            })
                        }
                    }
                    return text;
				}
			},
			{
				field : "CZ",
				title : "操作"
                ,width: 4
                ,formatter : function(value, row, index){
					var tableToolBar = $("#tableToolBar").html();
					return tableToolBar.replace(/USERID/g, row["USERID"]);
				}
			}
		]]
        ,onBeforeLoad: function(queryParams) {
            var url = $(this).datagrid("options")["url"];
			/*URL不等空时再发送请求*/
            if (!StringUtils.isEmpty(url)) {
                queryParams["pageSize"] = queryParams["rows"];
                queryParams["pageNumber"] = queryParams["page"];
                /*获取查询条件*/
                var formParams = getQueryFormValue();
                queryParams = $.extend(queryParams, formParams);
                return true;
            }else{
                return false;
            }
        }
	});
    $("body").on("click", ".tableToolBarBtn", function(){
        var action = $(this).attr("action");
        var USERID = $(this).attr("id");
        $("#dgForm").datagrid("selectRecord", USERID);
        if("update" == action){
            updateYljgry(USERID);
        }else if("delete" == action) {
            deleteYljgry(USERID);
        }else if("assignUser" == action){
            assignUserHandler(USERID)
		}
    })

    var readonly = !requestData["isManager"];
    $("#combogridYljg").combogrid({
        height: 30
		,width: 300
        ,panelWidth: 500
        ,singleSelect: true
        ,pagination: true
        ,idField: 'PTJGID'
        ,textField: 'YLJGMC'
        ,mode: "remote"
		,readonly: readonly
		,hasDownArrow: !readonly
        ,url: contextRoot + "/yljgxx/queryPageSummary.service"
        ,delay: 1000
        ,reversed: false
        ,columns:[[
            {
                field:'YLJGMC'
                ,title:'机构名称'
                ,width:240
            }
            ,{
                field:'XZQHMC'
                ,title:'行政区划'
                ,width:80
            }
            ,{
                field:'WSJGLBMC'
                ,title:'机构类别'
                ,width:80
            }
            ,{
                field:'YYDJMC'
                ,title:'机构等级'
                ,width:80
            }
        ]]
        ,onBeforeLoad: function(queryParams) {
            var url = $(this).datagrid("options")["url"];
			/*URL不等空时再发送请求*/
            if (!StringUtils.isEmpty(url)) {
                queryParams["pageSize"] = queryParams["rows"];
                queryParams["pageNumber"] = queryParams["page"];
                queryParams["YLJGMC"] = queryParams["q"];
                return true;
            }else{
                return false;
            }
        },
        onClickRow : function(rowIndex, rowData){
            lastSelectJgmc = rowData.YLJGMC;
			lastSelectJgid = rowData.PTJGID;
        },
    });

    if(readonly){
        //隐藏机构名称的查询条件
		$(".hidden").css('display',"none");
	}
}

/**获取查询表单的值*/
var lastSelectJgmc,lastSelectJgid;
function getQueryFormValue(){
    var params = {};
    var ary = $("#queryForm").serializeArray();
    $.each(ary, function(index, item){
        params[item["name"]] = item["value"];
	})
	/*combobox取值*/
		/* 处理：删除机构名称，马上点击查询的bug(此时，combobox还没有刷新值(延时bug)) */
		var PTJGID = $("#combogridYljg").combobox("getValue");
		var JGMC = $("#combogridYljg").combobox('getText');
		if(!$("#combogridYljg").combogrid("grid").datagrid("getSelected") || (JGMC != lastSelectJgmc && PTJGID == lastSelectJgid)){
			params.YLJGMC = JGMC;
		}else{
			params.PTJGID = PTJGID;
		}
		lastSelectJgmc = JGMC;
		lastSelectJgid = PTJGID;
    return params;
}

/**修改*/
function updateYljgry(USERID){
    var row = $("#dgForm").datagrid("getSelected");
    var USERID = row["USERID"];
    $.QuickUrlUtils.redirectUrl("manager.html?USERID="+USERID);
}
/**删除*/
function deleteYljgry(USERID){
    $.QuickAlert.alertConfirm({content:"将彻底删除该人员的相关信息，是否继续删除", callback: function(){
        var url = contextRoot + "/yljgry/deleteYljgry";
        $.QuickRemote.AjaxJson(url, function(result){
            if(result["state"])
            {
                $.QuickAlert.alertNormal("删除成功");
                $("#btnQuery").trigger("click");
            }
            else
            {
                $.QuickAlert.alertNormal("删除失败");
            }
        }, {USERID: USERID})
    }})
}

/**分配用户*/
function assignUserHandler(USERID){
    var row = $("#dgForm").datagrid("getSelected");
    StoreCache.setCache("cache_yljgry", row);
    $.QuickUrlUtils.redirectRootUrl("/user/manager.html");
}

/**查询按钮点击回调*/
function btnQueryHandler() {
    queryForm();
}

/**新增医疗机构人员*/
function btnAddHandler(){
    location = contextRoot + "/yljgry/manager.html";
}

/**查询按钮点击回调*/
function btnDownExcelHandler() {
	var formParams = getQueryFormValue();
	var $downloadForm = $("#downloadForm");
	$("#downloadForm").empty();
	$.each(formParams, function(key,val){
		var input = $("<input>").attr({name: key, value: val, type: 'hidden'});
		$downloadForm.append(input);
	})
	$downloadForm.submit();
}

</script>

<body class="easyui-layout">
	<div region="north" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9;">
		<div style="width: 100%; height: 100%; margin-left: 30px">
			<h3 style="line-height: 41px; margin: 0px; font-size: 18px;font-weight: bold;">机构人员列表</h3>
		</div>
	</div>
	<div region="center">
		<div id="tb" style="padding:8px 20px;">
			<form id="queryForm">
				<table>
					<tr>
						<td class="query-text hidden">机构名称</td>
						<td class="query-content hidden">
							<input id="combogridYljg">
						</td>
						<td class="query-text">姓名</td>
						<td class="query-content">
							<input name="XM" class="query-input">
						</td>
						<td class="query-text">工号</td>
						<td class="query-content">
							<input name="YSGH" class="query-input">
						</td>
						<td>
							<button id="btnQuery" class="btn btn-default" type="button" onclick="btnQueryHandler()">查询</button>
							<button id="btnAdd" class="btn btn-default" type="button" onclick="btnAddHandler()">新增</button>
							<button id="btnAdd" class="btn btn-default" type="button" onclick="btnDownExcelHandler()">导出</button>
						</td>
					</tr>
				</table>
			</form>
			
			<form id="downloadForm" action="downloadToExcel" enctype="multipart/form-data">
        	</form>
		</div>
		<table id="dgForm"></table>
	</div>
</body>
<script type="text/html" id="tableToolBar">
	<div>
		<a class="tableToolBarBtn" action="update" id=USERID href="javascript:void(0)" title="修改" style="color: #00c297;">修改</a>
		<!--<a class="tableToolBarBtn" action="delete" id=USERID href="javascript:void(0)" title="删除" style="color: #00c297;">删除</a>-->
		<!--<a class="tableToolBarBtn" action="assignUser" id=USERID href="javascript:void(0)" title="分配用户" style="color: #00c297;">分配用户</a>-->
	</div>
</script>
</html>



